﻿using System;
using System.Windows.Forms;
using SG.Sys.Console.CommandProcessing;
using SG.Sys.Console.Process;
using SG.Sys.Console.Tab;

namespace SG.Sys.Console.View
{
    partial class ConsolePane
    {

        private void ExecuteCommand()
        {
            var entry = ParseInputLine();

            if (entry.Command.IsEmpty) return;

            var stateChange = SendCommand(entry);
            var isDisplayingHistory = stateChange == StateChange.DisplayHistory; 
                
            if (!isDisplayingHistory)
            {
                var historyIndex = History.Add(entry);
                var historyIndexAnnotation = string.Format("[{0:d}] ", historyIndex);
                AppendText(textOutput, historyIndexAnnotation, new FormatOptions{PrettyPrint = true});
            }

            // if a stange change occured, we won't get an response, so manually adding one
            if (stateChange != StateChange.None)
            {
                var specialCommandLine = isDisplayingHistory
                                             ? string.Empty
                                             : entry.Command + Environment.NewLine;
                var text = specialCommandLine + Wrapper.PromptChar;
                AppendText(textOutput, text, new FormatOptions{PrettyPrint = true});
            }
        }

        private StateChange SendCommand(Entry entry)
        {
            var stateChange = Wrapper.SendCommand(entry);
            switch (stateChange)
            {
                case StateChange.UserIssuedExit:
                    SendClose();
                    break;

                case StateChange.TitleChange:
                    FireTitleChange(new TitleChangedEventArgs(Wrapper.Title));
                    break;

                case StateChange.DisplayHistory:
                    foreach (var iEntry in History)
                        AppendText(textOutput, iEntry);
                    break;
            }
            return stateChange;
        }

        private void ProcessKeyEvent(KeyEventArgs e)
        {
            if (e.Control)
                IsControlKeyHold = true;

            Entry entry = null;
            var regular = false;
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    MessageBox.Show("you want to run away!");
                    break;

                case Keys.Tab:
                    MessageBox.Show("no autocompletion yet");
                    break;

                case Keys.Down:
                    History.Down();
                    entry = History.SelectedOrLastEntry();
                    break;

                case Keys.Up:
                    History.Up();
                    entry = History.SelectedOrLastEntry();
                    break;

                case Keys.Enter:
                    ExecuteCommand();
                    break;

                default:
                    regular = true;
                    break;
            }

            if (entry != null)
                ReplaceInputText(entry.Command.ToString());

            e.SuppressKeyPress = !regular;
        }

        private void ReplaceInputText(string text)
        {
            textInput.Text = "";
            textInput.AppendText(text);
        }
    }
}
